package com.atguigu.gulimall.ware.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.ware.domain.response.ReceiveAddressResp;
import com.atguigu.gulimall.ware.feign.MemberFeignService;
import com.atguigu.gulimall.ware.vo.FreightVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;

import com.atguigu.gulimall.ware.dao.WareInfoDao;
import com.atguigu.gulimall.ware.entity.WareInfoEntity;
import com.atguigu.gulimall.ware.service.WareInfoService;
import org.springframework.util.StringUtils;


@Slf4j
@Service("wareInfoService")
public class WareInfoServiceImpl extends ServiceImpl<WareInfoDao, WareInfoEntity> implements WareInfoService {

    @Autowired
    MemberFeignService memberFeignService;
    
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    
        LambdaQueryWrapper<WareInfoEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            lambdaQueryWrapper.eq(WareInfoEntity::getId, key).or().likeRight(WareInfoEntity::getName, key);
        }
    
        IPage<WareInfoEntity> page = this.page(
                new Query<WareInfoEntity>().getPage(params),
                lambdaQueryWrapper
        );

        return new PageUtils(page);
    }

    @Override
    public FreightVO getFreight(Long addrId) {
        R receiveAddressResponse = memberFeignService.info(addrId);
        Integer code = receiveAddressResponse.getCode();
        if (code != 0) {
            log.error("WareInfoServiceImpl.getFreight get receiveAddress field response result: {}", JSONObject.toJSONString(receiveAddressResponse));
            return FreightVO.builder().build();
        }
        ReceiveAddressResp receiveAddressResp = receiveAddressResponse.getData("memberReceiveAddress", new TypeReference<ReceiveAddressResp>() {
        });
        log.info("get receive address result: {}", JSONObject.toJSONString(receiveAddressResp));
        if (receiveAddressResp == null || StringUtils.isEmpty(receiveAddressResp.getPhone())) {
            return FreightVO.builder().build();
        }
        String phone = receiveAddressResp.getPhone();
        String freightStr = phone.substring(phone.length() - 2, phone.length());
        BigDecimal freightPrice = new BigDecimal(freightStr);
        return FreightVO.builder()
                .address(receiveAddressResp)
                .freightPrice(freightPrice)
                .build();
    }

}